package com.ape.mapsa.produccion.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ape.mapsa.produccion.model.Item;
import com.ape.mapsa.produccion.model.Maquina;

@Repository
public class MaquinaDAOImpl extends GenericDAOImpl<Maquina, Long> implements MaquinaDAO {

	@Override
	@Transactional(readOnly = true)
	public Maquina getByCode(String maqCodigo) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Item.class);
		criteria.add(Restrictions.eq("codigo", maqCodigo));
		return (Maquina) criteria.uniqueResult();
	}

	@Override
	public void deleteMaq(Maquina maq) {
		maq.setIsActive(false);
		maq.setLastModified(new Date());
		update(maq);
	}

	@Override
	@Transactional(readOnly = true)
	public List<Maquina> searchMaquinaByCode(String codigo) {
		@SuppressWarnings("unchecked")
		List<Maquina> result = sessionFactory.getCurrentSession().createCriteria(Maquina.class).add(Restrictions.like("maqCodigo", codigo)).list();
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<Maquina> getMaquinasByType(Integer valor) {
		return sessionFactory.getCurrentSession().createCriteria(Maquina.class).add(Restrictions.eq("maqTipo", valor)).list();
	}

}
